import sa from './global'
import { isEmptyObject, isObject, extend, isArray, each, include, getUUID, toArray } from '../utils/index'

import './datasend.js'

// request 直接调用 sa.request

sa.log = function () {
  if (sa.para.show_log) {
    if (typeof console === 'object' && console.log) {
      try {
        var arg = Array.prototype.slice.call(arguments)
        return console.log.apply(console, arg)
      } catch (e) {
        console.log(arguments[0])
      }
    }
  }
}

sa.track = function (e, p, c) {
  sa.prepareData(
    {
      type: 'track',
      event: e,
      properties: p
    },
    c
  )
}

sa.setProfile = function (p) {
  sa.prepareData({
    type: 'profile_set',
    properties: p
  })
}

sa.setOnceProfile = function (p, c) {
  sa.prepareData(
    {
      type: 'profile_set_once',
      properties: p
    },
    c
  )
}

sa.login = function (id) {
  var firstId = sa.store.getFirstId()
  var distinctId = sa.store.getDistinctId()
  if (id !== distinctId) {
    if (firstId) {
      sa.trackSignup(id, '$SignUp')
    } else {
      sa.store.set('first_id', distinctId)
      sa.trackSignup(id, '$SignUp')
    }
  }
}

sa.logout = function (isChangeId) {
  var firstId = sa.store.getFirstId()
  if (firstId) {
    sa.store.set('first_id', '')
    if (isChangeId === true) {
      sa.store.set('distinct_id', getUUID())
    } else {
      sa.store.set('distinct_id', firstId)
    }
  } else {
    sa.log('没有first_id，logout失败')
  }
}

if (SA_PLATFORM == 'sa-weixin') {
  sa.identify = function (id) {
    console.log(SA_PLATFORM, '___weixin')
    if (typeof id === 'number') {
      id = String(id)
    } else if (typeof id !== 'string') {
      return false
    }
    var firstId = sa.store.getFirstId()
    if (firstId) {
      sa.store.set('first_id', id)
    } else {
      sa.store.set('distinct_id', id)
    }
  }
}

if (SA_PLATFORM == 'sa-baidu') {
  console.log(SA_PLATFORM, '___baidu')
  sa.identify = function (id) {
    if (typeof id === 'number') {
      id = String(id)
    } else if (typeof id !== 'string') {
      return false
    }
    var firstId = sa.store.getFirstId()
    if (firstId) {
      sa.store.set('first_id', id)
    } else {
      sa.store.set('distinct_id', id)
    }
  }
}

sa.trackSignup = function (id, e, p) {
  sa.prepareData({
    original_id: sa.store.getFirstId() || sa.store.getDistinctId(),
    distinct_id: id,
    type: 'track_signup',
    event: e,
    properties: p
  })
  sa.store.set('distinct_id', id)
}

sa.registerApp = function (obj) {
  if (isObject(obj) && !isEmptyObject(obj)) {
    sa.currentProps = extend(sa.currentProps, obj)
  }
}

sa.clearAppRegister = function (arr) {
  if (isArray(arr)) {
    each(sa.currentProps, function (value, key) {
      if (include(arr, key)) {
        delete sa.currentProps[key]
      }
    })
  }
}

sa.use = function (plugin) {
  const installedPlugins = this._installedPlugins || (this._installedPlugins = [])
  if (installedPlugins.indexOf(plugin) > -1) {
    return this
  }
  const args = toArray(arguments, 1)
  args.unshift(this)
  if (typeof plugin.init === 'function') {
    plugin.init.apply(plugin, args)
  } else if (typeof plugin === 'function') {
    plugin.apply(null, args)
  }
  installedPlugins.push(plugin)
  return this
}
